#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utypes.h"
#include "sprite.h"
#include "kbc.h"
#include "queue.h"
#include "ints.h"
#include "GQueue.h"
#include "rtc.h"
#include "gameDesign.h"

Queue keys_queue;
void kbd_isr(void);
extern int scancode;
extern int temp_aux;
char * video_mem;
extern int ** gameMat;
char name[255];
void new_game(char * video_mem){
	
	draw_string("A new Game has begun!",HRES/2-15*4, 140, WHITE, BLACK, video_mem);
	gState = Waiting;
	tempo_aux=1000;
	waitState=Begin;
	gameControl(video_mem);
	
}
void high_score(){
	
	Pontuacoes  * p = read_pontuacoes();
	int count=0;
	for(;count < 10 && strcmp(p[count].nome,"NULL")!=0;count++){
		char tmp[500];
		sprintf(tmp,"%s - %d",p[count].nome,p[count].pontua);
		draw_string(tmp, HRES/2-50, 20*count+140, WHITE, BLACK, video_mem);
		
	}
	gState = Waiting;
	tempo_aux=1000;
	waitState=Menu;
	gameControl(video_mem);
}



int main(int argc, char * argv[]){
	
	if(argc!=2){
		puts("uso: trap_em_all <nome>");
		return 1;
	}
	
	strcpy(name,argv[1]);
	int mode=0x0103;
	__dpmi_meminfo map;

	CriaMusicas("plug.txt");
	
	state = Play;
	int arrow_pos = 0;
	int arrow_y = 145;

	_go32_dpmi_seginfo old_kbd_irq;
	//queueInit(&keys_queue);
	install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
	
	int sair = 0;
	
	
	
	
	video_mem = enter_graphics(mode, &map);
	
	
	rtc_enable();
	
	
	//draw_frame();
	while(!sair){
	clear_screen(BLACK,video_mem);
	draw_string("TRAP 'EM ALL", HRES/2-15*4, 80, RED, BLACK, video_mem);
	draw_string("New Game", HRES/2-15*4, 140, WHITE, BLACK, video_mem);
	draw_string("High Scores", HRES/2-15*4, 160, WHITE, BLACK, video_mem);
	draw_string("Exit", HRES/2-15*4, 180, WHITE, BLACK, video_mem);
	
	Sprite *s = create_sprite(arrow, video_mem,HRES/2-15*5,arrow_y);	
	int last_sc =0;
	do
	{
		//if(!queueEmpty(&keys_queue))
		//{
			//obter o proximo elemento da queue e retira-lo
			if(scancode!=last_sc){
				last_sc = scancode;
			
					
				if(last_sc ==UpKey)				
					arrow_pos--;
				
					if(last_sc ==DownKey)
					arrow_pos++;
				erase_sprite(s,video_mem,BLACK);
				if (arrow_pos>2)
					arrow_pos =0;
				if (arrow_pos<0)
					arrow_pos =2;	
				draw_string("New Game", HRES/2-15*4, 140, WHITE, BLACK, video_mem);
				draw_string("High Scores", HRES/2-15*4, 160, WHITE, BLACK, video_mem);
				draw_string("Exit", HRES/2-15*4, 180, WHITE, BLACK, video_mem);	
				s->y =arrow_y+20*arrow_pos;
				draw_sprite(s,video_mem);
			
			}
		
				
	} while(last_sc != 0x1c);
	
	clear_screen(BLACK,video_mem);
	switch(arrow_pos){
		case 0: new_game(video_mem);
			break;
		case 1: high_score();
			break;
		case 2 : sair = 1;
		default :break;
	}
	}
	
	reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
	clear_screen(BLACK,video_mem);
	
	leave_graphics(map);	
	speaker_off();
	rtc_disable();
	//printGameMatrix();	
	
	return 0;
}
